package com.jzr.bedside.base

import android.app.Activity
import android.content.Intent
import android.content.pm.ActivityInfo
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import com.jzr.bedside.R
import com.jzr.bedside.view.LoadingDialog
import com.jzr.bedside.view.SwipeBackActivity.SwipeBackActivity

abstract class BaseActivity : SwipeBackActivity() {

    private var loadingDialog: LoadingDialog? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //取消状态栏
        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        )

        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        val params: WindowManager.LayoutParams = window.attributes
        params.systemUiVisibility =
            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_IMMERSIVE
        window.attributes = params
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)

        setContentView(getLayoutId())
        attachView()
        initView()

    }

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        if (hasFocus) {
            window.decorView.systemUiVisibility =
                (View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        detachView()
    }

    private fun finishActivity() {
        overridePendingTransition(R.anim.slide_right_in, R.anim.slide_right_out)
    }

    fun startActivityIn(
        intent: Intent?,
        curAct: Activity
    ) {
        if (intent != null) {
            curAct.startActivity(intent)
            curAct.overridePendingTransition(R.anim.slide_left_in, R.anim.slide_left_out)
        }
    }

    override fun finish() {
        super.finish()
        finishActivity()
    }

    open fun showLoadingDialog(text :String?) {
        dismissLoadingDialog()
        val builder:LoadingDialog.Builder =
          LoadingDialog.Builder(this)
                .setMessage(text)
                .setCancelable(true)
        loadingDialog = builder.create()
        loadingDialog!!.show()
    }

    open fun dismissLoadingDialog() {
        if (null != loadingDialog && loadingDialog!!.isShowing) {
            loadingDialog!!.dismiss()
            loadingDialog = null
        }
    }

    abstract fun getLayoutId(): Int

    abstract fun attachView()

    abstract fun detachView()

    abstract fun initView()


}